﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MapEditor.Forms
{
    public partial class LoadMapForm : Form
    {
        //Declare the load file name
        string loadFileName;

        public LoadMapForm()
        {
            InitializeComponent();
        }

        private void browseButton_Click(object sender, EventArgs e)
        {
            //Set the initial directory
            openFileDialog1.InitialDirectory = @"C:\";

            //Set the filter for the text files only
            openFileDialog1.Title = "Select a Map File";
            openFileDialog1.FilterIndex = 1;

            //Get the load file name
            if (openFileDialog1.ShowDialog() != DialogResult.Cancel)
            {
                loadFileName = openFileDialog1.FileName;
                fileName.Text = loadFileName;
            }
            else
            {
                loadFileName = "";
            }

        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            //Return the cancel dialog result
            this.DialogResult = DialogResult.Cancel;
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (Game1.Front1tileSheetFileName != null && Game1.Front2tileSheetFileName != null && Game1.Front3tileSheetFileName != null && Game1.PlayertileSheetFileName != null && Game1.EnemytileSheetFileName != null && Game1.ScreentileSheetFileName != null && Game1.Back1tileSheetFileName != null && Game1.Back2tileSheetFileName != null && Game1.Back3tileSheetFileName != null)
            {
                Game1.map.Loadmap(loadFileName);
            }
            else
            {
                MessageBox.Show("Please load a tileset before loading a map");
            }

            //Update the map size and tile dimensions
            Game1.FrontLayer1Height = Game1.map.FrontLayer1Height;
            Game1.FrontLayer1Width = Game1.map.FrontLayer1Width;
            Game1.Front1TileHeight = Game1.map.FrontLayer1TileHeight;
            Game1.Front1TileWidth = Game1.map.FrontLayer1TileWidth;

            Game1.FrontLayer2Height = Game1.map.FrontLayer2Height;
            Game1.FrontLayer2Width = Game1.map.FrontLayer2Width;
            Game1.Front2TileHeight = Game1.map.FrontLayer2TileHeight;
            Game1.Front2TileWidth = Game1.map.FrontLayer2TileWidth;

            Game1.EnemyLayerHeight = Game1.map.EnemyLayerHeight;
            Game1.EnemyLayerWidth = Game1.map.EnemyLayerWidth;
            Game1.Front3TileHeight = Game1.map.FrontLayer3TileHeight;
            Game1.Front3TileWidth = Game1.map.FrontLayer3TileWidth;

            Game1.PlayerLayerHeight = Game1.map.PlayerLayerHeight;
            Game1.PlayerLayerWidth = Game1.map.PlayerLayerWidth;
            Game1.PlayerTileHeight = Game1.map.PlayerTileHeight;
            Game1.PlayerTileWidth = Game1.map.PlayerTileWidth;

            Game1.ScreenLayerHeight = Game1.map.ScreenLayerHeight;
            Game1.ScreenLayerWidth = Game1.map.ScreenLayerWidth;
            Game1.ScreenTileHeight = Game1.map.ScreenTileHeight;
            Game1.ScreenTileWidth = Game1.map.ScreenTileWidth;

            Game1.BackgroundLayer1Height = Game1.map.BackgroundLayer1Height;
            Game1.BackgroundLayer1Width = Game1.map.BackgroundLayer1Width;
            Game1.Back1TileHeight = Game1.map.Background1TileHeight;
            Game1.Back1TileWidth = Game1.map.Background1TileWidth;

            Game1.BackgroundLayer2Height = Game1.map.BackgroundLayer2Height;
            Game1.BackgroundLayer2Width = Game1.map.BackgroundLayer2Width;
            Game1.Back2TileHeight = Game1.map.Background2TileHeight;
            Game1.Back2TileWidth = Game1.map.Background2TileWidth;

            Game1.BackgroundLayer3Height = Game1.map.BackgroundLayer3Height;
            Game1.BackgroundLayer3Width = Game1.map.BackgroundLayer3Width;
            Game1.Back3TileHeight = Game1.map.Background3TileHeight;
            Game1.Back3TileWidth = Game1.map.Background3TileWidth;


            //Update the map size and dimensions
            Game1.map = new Map_Classes.Map(Game1.FrontLayer1Height, Game1.FrontLayer1Width, Game1.Front1TileHeight, Game1.Front1TileWidth, Game1.Front1LayerType, Game1.FrontLayer2Height, Game1.FrontLayer2Width, Game1.Front2TileHeight, Game1.Front2TileWidth, Game1.Front2LayerType, Game1.FrontLayer3Height, Game1.FrontLayer3Width, Game1.Front3TileHeight, Game1.Front3TileWidth, Game1.Front3LayerType, 
                Game1.ScreenLayerHeight, Game1.ScreenLayerWidth, Game1.ScreenTileHeight, Game1.ScreenTileWidth, Game1.ScreenLayerType, Game1.PlayerLayerHeight, Game1.PlayerLayerWidth, Game1.PlayerTileHeight, Game1.PlayerTileWidth, Game1.PlayerLayerType, Game1.EnemyLayerHeight, Game1.EnemyLayerWidth, Game1.EnemyTileHeight, Game1.EnemyTileWidth, Game1.EnemyLayerType, Game1.BackgroundLayer1Height,
                Game1.BackgroundLayer1Width, Game1.Back1TileHeight, Game1.Back1TileWidth, Game1.Back1LayerType, Game1.BackgroundLayer2Height, Game1.BackgroundLayer2Width, Game1.Back2TileHeight, Game1.Back2TileWidth, Game1.Back2LayerType, Game1.BackgroundLayer3Height, Game1.BackgroundLayer3Width, Game1.Back3TileHeight, Game1.Back3TileWidth, Game1.Back3LayerType);

            //Load the map
            Game1.map.Loadmap(loadFileName);

            //Reload the tileset
            Game1.map.LoadTileSet(Game1.Front1TileSheet, Game1.Front1TileWidth, Game1.Front1TileHeight);
            Game1.map.LoadTileSet(Game1.Front2TileSheet, Game1.Front2TileWidth, Game1.Front2TileHeight);
            Game1.map.LoadTileSet(Game1.Front3TileSheet, Game1.Front3TileWidth, Game1.Front3TileHeight);
            Game1.map.LoadTileSet(Game1.PlayerTileSheet, Game1.PlayerTileWidth, Game1.PlayerTileHeight);
            Game1.map.LoadTileSet(Game1.EnemyTileSheet, Game1.EnemyTileWidth, Game1.EnemyTileHeight);
            Game1.map.LoadTileSet(Game1.ScreenTileSheet, Game1.ScreenTileWidth, Game1.ScreenTileHeight);
            Game1.map.LoadTileSet(Game1.Back1TileSheet, Game1.Back1TileWidth, Game1.Back1TileHeight);
            Game1.map.LoadTileSet(Game1.Back2TileSheet, Game1.Back2TileWidth, Game1.Back2TileHeight);
            Game1.map.LoadTileSet(Game1.Back3TileSheet, Game1.Back3TileWidth, Game1.Back3TileHeight);

            this.DialogResult = DialogResult.OK;
        }
    }
}
